package com.guihuo.mybatis.first;

import com.guihuo.mybatis.first.beans.Test;
import com.guihuo.mybatis.first.mapper.TestMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;

/**
 * 项目名称：mybatis-learning
 * 类名称：FirstClass
 * 类描述：FirstClass
 * 创建时间：2018/9/20
 *
 * @author guihuo   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public class FirstClass {

    public static void main(String[] args) throws FileNotFoundException {
        TestMapper testMapper = getSqlSession().getMapper(TestMapper.class);
        Test test = testMapper.selectByPrimaryKey(1);
    }

    public static SqlSession getSqlSession() throws FileNotFoundException {
        //配置文件
        URL url = FirstClass.class.getClassLoader().getResource("mybatis-config.xml");
        InputStream configFile = new FileInputStream(url.getPath());
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile);
        //加载配置文件得到SqlSessionFactory
        return sqlSessionFactory.openSession();
    }

}
